<?php
    if(isset($_POST['setTag'])):
        $this->session->set('tag',1);
        if(isset($_POST['tag'])) {
            $_SESSION['tag_list'] = array();
            $iter=0;
            foreach($_POST['tag'] as $new_tag):
                $_SESSION['tag_list'][$iter] = $new_tag;
                $iter++;
            endforeach;
        } else {
            if(isset($_SESSION['tag_list'])) {
                $this->session->remove('tag_list');
            }
        }
?>
    <script language="javascript">
        self.close();
        window.opener.location.reload();
    </script>
<?php else: if(isset($_SESSION['tag'])): $this->session->remove('tag'); endif; ?>

<h1>Choose User</h1>
<form action="" method="post" name="tagForm">
    <?php foreach($users['user'] as $user):
            $checked=0;
            if(isset($_SESSION['tag_list'])):
                foreach ($this->session->get('tag_list') as $tag):
                    if($user["User"]["name"] == $tag):
                        $checked=0;
                    endif;
                endforeach;
            endif;
            if($checked==1):
    ?>
        <input checked="true" type="checkbox" name="tag[]" value="<?php echo $user["User"]["name"] ?>"><?php echo $user["User"]["name"] ?><br/>
    <?php   else:  ?>
        <input type="checkbox" name="tag[]" value="<?php echo $user["User"]["name"] ?>"><?php echo $user["User"]["name"] ?><br/>
    <?php   endif;
        endforeach;
    ?>
    <?php foreach($users['following'] as $user):
            $checked=0;
            if(isset($_SESSION['tag_list'])):
                foreach ($this->session->get('tag_list') as $tag):
                    if($user["User"]["name"] == $tag):
                        $checked=0;
                    endif;
                endforeach;
            endif;
            if($checked==1):
    ?>
        <input checked="true" type="checkbox" name="tag[]" value="<?php echo $user["User"]["name"] ?>"><?php echo $user["User"]["name"] ?><br/>
    <?php   else:  ?>
        <input type="checkbox" name="tag[]" value="<?php echo $user["User"]["name"] ?>"><?php echo $user["User"]["name"] ?><br/>
    <?php   endif;
        endforeach;
    ?>
        <input type="submit" value="Submit" name="setTag" />
</form>
<?php endif ?>